import 'package:flutter/material.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:weichat_post/entity/user.dart';
import 'package:weichat_post/pages/index.dart';
import 'package:weichat_post/utils/index.dart';
import 'package:weichat_post/widgets/index.dart';

class TimelinePage extends StatefulWidget {
  const TimelinePage({super.key});

  @override
  State<TimelinePage> createState() => _TimelinePageState();
}

class _TimelinePageState extends State<TimelinePage> {
  UserModel? _user;

  @override
  void initState() {
    super.initState();
    _user = UserModel(
      avator: "https://i.ibb.co/Ss53Mvz/38721732002159-pic.jpg",
      nickname: "彬彬",
      cover: "https://i.ibb.co/RCs3Wc0/38731732002164-pic.jpg",
    );
    if (mounted) {
      setState(() {});
    }
  }

  Widget _buildHeader() {
    final width = MediaQuery.of(context).size.width;
    return _user?.cover == null
        ? const Text("加载中")
        : Stack(
            children: [
              // 背景
              Padding(
                padding: const EdgeInsets.only(bottom: 20),
                child: SizedBox(
                  width: width,
                  child: Image.network(
                    _user?.cover ?? "",
                    height: width * 0.75,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              // 昵称 、头像
              Positioned(
                right: spacing,
                bottom: 5,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(right: spacing),
                      child: Text(
                        _user?.nickname ?? "",
                        style: const TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                          letterSpacing: 2,
                          shadows: [
                            Shadow(
                              color: Colors.black,
                              offset: Offset(1, 1),
                              blurRadius: 2,
                            ),
                          ],
                        ),
                      ),
                    ),
                    Container(
                      clipBehavior: Clip.antiAlias,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Image.network(
                        _user?.avator ?? "",
                        height: 65,
                        width: 65,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          );
  }

  Widget _mainView() {
    return Column(
      children: [
        _buildHeader(),
      ],
    );
  }

  // 发布
  _onPublish() async {
    final result = await DuBottomSheet().wxPicker<List<AssetEntity>>(context);
    if (result == null || result.isEmpty) {
      return;
    }
    Navigator.of(context).push(
      MaterialPageRoute(builder: ((context) {
        return PostEditPage(
          postType: (result.length == 1 && result.first.type == AssetType.video)
              ? PostType.video
              : PostType.image,
          selectedAssets: result,
        );
      })),
    );
  } 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBarWidget(
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: spacing),
            child: GestureDetector(
              onTap: (_onPublish),
              child: const Icon(
                Icons.camera_alt,
                color: Colors.white70,
              ),
            ),
          ),
        ],
      ),
      body: _mainView(),
    );
  }
}
